﻿using MyServer.Common.Tools;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyServer.Common
{
    /// <summary>
    /// 表示数据操作器基类
    /// </summary>
    public abstract class DataOperatorBase : QueueOperateItemBase
    {

        #region ==========属性==========

        /// <summary>
        /// 表示与处理器关联的数据中心
        /// </summary>
        public DataCenterBase DataCenter { get; private set; }

        #endregion

        #region========抽象函数========

        /// <summary>
        /// 开始尝试处理指定包中的数据
        /// </summary>
        /// <param name="pkg">要处理的数据包列表</param>
        /// <param name="RePkg">表示处理完毕要发送的数据包</param>
        /// <returns>是否成功完成一次操作</returns>
        protected abstract OperateResult Operate(ref DataPackageCollection pkg, out DataPackageCollection RePkg);

        #endregion

        #region========构造函数========

        public DataOperatorBase(DataCenterBase Center)
        {
            this.DataCenter = Center;
        }

        #endregion

        #region========重写方法========

        protected override OperateResult DoOperateSetp(DataPackageCollection pkg,out DataPackageCollection repkg)
        {
            pkg.State = PackageCollectionState.ReadyForOperate;
            return Operate(ref pkg, out repkg);
        }

        protected override bool BeforeOperateSetp(DataPackageCollection pkg)
        {
            return true;
        }

        protected override bool AfterOperateSetp(DataPackageCollection pkg)
        {
            if (pkg.State == PackageCollectionState.Operating)
            {
                return true;
            }
            return false;
        }

        #endregion

    }

}
